﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CPPEI.WPF
{
    public partial class MainSubSplitContainer : Control
    {
        static MainSubSplitContainer()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MainSubSplitContainer), new FrameworkPropertyMetadata(typeof(MainSubSplitContainer)));
        }

        /// <summary>
        /// 主视图
        /// </summary>
        public View MainView
        {
            get
            {
                return (View)GetValue(MainViewProperty);
            }
            set
            {
                SetValue(MainViewProperty, value);
            }
        }

        /// <summary>
        /// 子视图
        /// </summary>
        public View SubView
        {
            get
            {
                return (View)GetValue(SubViewProperty);
            }
            set
            {
                SetValue(SubViewProperty, value);
            }
        }

        public double SubViewWidth
        {
            get
            {
                return (double)GetValue(SubViewWidthProperty);
            }
            set
            {
                SetValue(SubViewWidthProperty, value);
            }
        }

        /// <summary>
        /// 动画时间（秒)默认0.5
        /// </summary>
        public double AnimationDuration
        {
            get
            {
                return (double)GetValue(AnimationDurationProperty);
            }
            set
            {
                SetValue(AnimationDurationProperty, value);
            }
        }

        Storyboard _storyboard;
        DoubleAnimation _doubleAnimationSubViewLeft;
        DoubleAnimation _doubleAnimationMainViewTop;
        DoubleAnimation _doubleAnimationMainViewLeft;
        DoubleAnimation _doubleAnimationMainViewScale;
        DoubleAnimation _doubleAnimationMainViewScaleY;

        enum ViewMode
        {
            Sub = 0x01,
            Main = 0x02,
        }

        private ViewMode _viewMode;
        private bool _isRunning = false;

        /// <summary>
        /// 显示子视图
        /// </summary>
        public void ShowSubView()
        {
            if (_isRunning || _viewMode== ViewMode.Sub)
            {
                return;
            }

            _viewMode = ViewMode.Sub;

            SubView.Freeze();
            MainView.Freeze();

            Grid subViewContainer = this.Template.FindName("subViewContainer", this) as Grid;
            Grid mainViewContainer = this.Template.FindName("mainViewContainer", this) as Grid;

            Rectangle subViewVisual = this.Template.FindName("subViewVisual", this) as Rectangle;
            subViewVisual.Fill = new ImageBrush(SubView.Snapshot);
            Rectangle mainViewVisual = this.Template.FindName("mainViewVisual", this) as Rectangle;
            mainViewVisual.Fill = new ImageBrush(MainView.Snapshot);

            MainView.Deactive();

            subViewContainer.Visibility = System.Windows.Visibility.Hidden;
            mainViewContainer.Visibility = System.Windows.Visibility.Hidden;

            subViewVisual.Visibility = System.Windows.Visibility.Visible;
            mainViewVisual.Visibility = System.Windows.Visibility.Visible;

            double destMainVisualHeight = this.ActualHeight * 0.8d;
            double mainVisualTop = (this.ActualHeight - destMainVisualHeight) / 2.0d;

            if (_storyboard == null)
            {
                _storyboard = new Storyboard();
                _storyboard.Completed += _storyboard_Completed;

                _doubleAnimationSubViewLeft = new DoubleAnimation();
                Storyboard.SetTarget(_doubleAnimationSubViewLeft, subViewVisual);
                Storyboard.SetTargetProperty(_doubleAnimationSubViewLeft, new PropertyPath(Canvas.LeftProperty));

                _doubleAnimationMainViewLeft = new DoubleAnimation();
                Storyboard.SetTarget(_doubleAnimationMainViewLeft, mainViewVisual);
                Storyboard.SetTargetProperty(_doubleAnimationMainViewLeft, new PropertyPath(Canvas.LeftProperty));

                _doubleAnimationMainViewTop = new DoubleAnimation();
                Storyboard.SetTarget(_doubleAnimationMainViewTop, mainViewVisual);
                Storyboard.SetTargetProperty(_doubleAnimationMainViewTop, new PropertyPath(Canvas.TopProperty));

                _doubleAnimationMainViewScale = new DoubleAnimation();
                Storyboard.SetTarget(_doubleAnimationMainViewScale, mainViewVisual);
                Storyboard.SetTargetProperty(_doubleAnimationMainViewScale, new PropertyPath("(Rectangle.RenderTransform).(ScaleTransform.ScaleX)"));

                _doubleAnimationMainViewScaleY = new DoubleAnimation();
                Storyboard.SetTarget(_doubleAnimationMainViewScaleY, mainViewVisual);
                Storyboard.SetTargetProperty(_doubleAnimationMainViewScaleY, new PropertyPath("(Rectangle.RenderTransform).(ScaleTransform.ScaleY)"));

                _storyboard.Children.Add(_doubleAnimationSubViewLeft);
                _storyboard.Children.Add(_doubleAnimationMainViewTop);
                _storyboard.Children.Add(_doubleAnimationMainViewLeft);
                _storyboard.Children.Add(_doubleAnimationMainViewScale);
                _storyboard.Children.Add(_doubleAnimationMainViewScaleY);
            }

            _doubleAnimationSubViewLeft.From = -1 * SubViewWidth;
            _doubleAnimationSubViewLeft.To = 0;
            _doubleAnimationSubViewLeft.Duration = new Duration(TimeSpan.FromSeconds(AnimationDuration));

            _doubleAnimationMainViewLeft.From = 0;
            _doubleAnimationMainViewLeft.To = SubViewWidth;
            _doubleAnimationMainViewLeft.Duration = new Duration(TimeSpan.FromSeconds(AnimationDuration));

            _doubleAnimationMainViewTop.From = 0;
            _doubleAnimationMainViewTop.To = mainVisualTop;
            _doubleAnimationMainViewTop.Duration = new Duration(TimeSpan.FromSeconds(AnimationDuration));

            _doubleAnimationMainViewScale.From = 1.0d;
            _doubleAnimationMainViewScale.To = 0.8d;
            _doubleAnimationMainViewScale.Duration = new Duration(TimeSpan.FromSeconds(AnimationDuration));

            _doubleAnimationMainViewScaleY.From = 1.0d;
            _doubleAnimationMainViewScaleY.To = 0.8d;
            _doubleAnimationMainViewScaleY.Duration = new Duration(TimeSpan.FromSeconds(AnimationDuration));

            _storyboard.Begin(this);
            _isRunning = true;
        }

        void _storyboard_Completed(object sender, EventArgs e)
        {
            if (_viewMode == ViewMode.Sub)
            {
                Grid subViewContainer = this.Template.FindName("subViewContainer", this) as Grid;
                Rectangle subViewVisual = this.Template.FindName("subViewVisual", this) as Rectangle;
                subViewContainer.Visibility = System.Windows.Visibility.Visible;
                subViewVisual.Visibility = System.Windows.Visibility.Hidden;
                SubView.Active();
            }
            else if (_viewMode == ViewMode.Main)
            {
                Grid mainViewContainer = this.Template.FindName("mainViewContainer", this) as Grid;
                Rectangle mainViewVisual = this.Template.FindName("mainViewVisual", this) as Rectangle;
                mainViewContainer.Visibility = System.Windows.Visibility.Visible;
                mainViewVisual.Visibility = System.Windows.Visibility.Hidden;
                MainView.Active();
            }
            _isRunning = false;
        }

        /// <summary>
        /// 显示主视图
        /// </summary>
        public void ShowMainView()
        {
            if (_isRunning || _viewMode == ViewMode.Main)
            {
                return;
            }

            _viewMode = ViewMode.Main;

            SubView.Freeze();
            MainView.Freeze();

            Grid subViewContainer = this.Template.FindName("subViewContainer", this) as Grid;
            Grid mainViewContainer = this.Template.FindName("mainViewContainer", this) as Grid;


            Rectangle subViewVisual = this.Template.FindName("subViewVisual", this) as Rectangle;
            subViewVisual.Fill = new ImageBrush(SubView.Snapshot);
            Rectangle mainViewVisual = this.Template.FindName("mainViewVisual", this) as Rectangle;
            mainViewVisual.Fill = new ImageBrush(MainView.Snapshot);


            SubView.Deactive();

            subViewContainer.Visibility = System.Windows.Visibility.Hidden;
            mainViewContainer.Visibility = System.Windows.Visibility.Hidden;

            subViewVisual.Visibility = System.Windows.Visibility.Visible;
            mainViewVisual.Visibility = System.Windows.Visibility.Visible;

            //double destMainVisualHeight = this.ActualHeight * 0.8d;
            //double mainVisualTop = (this.ActualHeight - destMainVisualHeight) / 2.0d;

            double curSubVisualLeft = Canvas.GetLeft(subViewVisual);
            double curMainVisualTop = Canvas.GetTop(mainViewVisual);
            double curMainVisualLeft = Canvas.GetLeft(mainViewVisual);

            if (_storyboard == null)
            {
                _storyboard = new Storyboard();
                _storyboard.Completed += _storyboard_Completed;

                _doubleAnimationSubViewLeft = new DoubleAnimation();
                Storyboard.SetTarget(_doubleAnimationSubViewLeft, subViewVisual);
                Storyboard.SetTargetProperty(_doubleAnimationSubViewLeft, new PropertyPath(Canvas.LeftProperty));

                _doubleAnimationMainViewLeft = new DoubleAnimation();
                Storyboard.SetTarget(_doubleAnimationMainViewLeft, mainViewVisual);
                Storyboard.SetTargetProperty(_doubleAnimationMainViewLeft, new PropertyPath(Canvas.LeftProperty));

                _doubleAnimationMainViewTop = new DoubleAnimation();
                Storyboard.SetTarget(_doubleAnimationMainViewTop, mainViewVisual);
                Storyboard.SetTargetProperty(_doubleAnimationMainViewTop, new PropertyPath(Canvas.TopProperty));

                _doubleAnimationMainViewScale = new DoubleAnimation();
                Storyboard.SetTarget(_doubleAnimationMainViewScale, mainViewVisual);
                Storyboard.SetTargetProperty(_doubleAnimationMainViewScale, new PropertyPath("(Rectangle.RenderTransform).(ScaleTransform.ScaleX)"));

                _doubleAnimationMainViewScaleY = new DoubleAnimation();
                Storyboard.SetTarget(_doubleAnimationMainViewScaleY, mainViewVisual);
                Storyboard.SetTargetProperty(_doubleAnimationMainViewScaleY, new PropertyPath("(Rectangle.RenderTransform).(ScaleTransform.ScaleY)"));

                _storyboard.Children.Add(_doubleAnimationSubViewLeft);
                _storyboard.Children.Add(_doubleAnimationMainViewTop);
                _storyboard.Children.Add(_doubleAnimationMainViewLeft);
                _storyboard.Children.Add(_doubleAnimationMainViewScale);
                _storyboard.Children.Add(_doubleAnimationMainViewScaleY);
            }

            _doubleAnimationSubViewLeft.From = curSubVisualLeft;
            _doubleAnimationSubViewLeft.To = -1 * SubViewWidth;
            _doubleAnimationSubViewLeft.Duration = new Duration(TimeSpan.FromSeconds(AnimationDuration));

            _doubleAnimationMainViewLeft.From = curMainVisualLeft;
            _doubleAnimationMainViewLeft.To = 0;
            _doubleAnimationMainViewLeft.Duration = new Duration(TimeSpan.FromSeconds(AnimationDuration));

            _doubleAnimationMainViewTop.From = curMainVisualTop;
            _doubleAnimationMainViewTop.To = 0;
            _doubleAnimationMainViewTop.Duration = new Duration(TimeSpan.FromSeconds(AnimationDuration));

            _doubleAnimationMainViewScale.From = 0.8d;
            _doubleAnimationMainViewScale.To = 1.0d;
            _doubleAnimationMainViewScale.Duration = new Duration(TimeSpan.FromSeconds(AnimationDuration));

            _doubleAnimationMainViewScaleY.From = 0.8d;
            _doubleAnimationMainViewScaleY.To = 1.0d;
            _doubleAnimationMainViewScaleY.Duration = new Duration(TimeSpan.FromSeconds(AnimationDuration));

            _storyboard.Begin(this);
            _isRunning = true;
        }

        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            if (_viewMode == ViewMode.Sub)
            {
            }
            else if (_viewMode == ViewMode.Main)
            {
            }
            base.OnRenderSizeChanged(sizeInfo);
        }

        protected override void OnPreviewTouchDown(System.Windows.Input.TouchEventArgs e)
        {
            if (_viewMode == ViewMode.Sub)
            {
                Point pt = e.GetTouchPoint(this).Position;
                Grid subViewContainer = this.Template.FindName("subViewContainer", this) as Grid;
                Point subViewPosition = subViewContainer.TranslatePoint(new Point(0, 0), this);
                if (pt.X > subViewPosition.X + SubViewWidth)
                {
                    ShowMainView();
                }
            }
            base.OnPreviewTouchDown(e);
        }

        protected override void OnPreviewMouseDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            if (_viewMode == ViewMode.Sub)
            {
                Point pt = e.GetPosition(this);
                Grid subViewContainer = this.Template.FindName("subViewContainer", this) as Grid;
                Point subViewPosition = subViewContainer.TranslatePoint(new Point(0, 0), this);
                if (pt.X > subViewPosition.X + SubViewWidth)
                {
                    ShowMainView();
                }
            }
            base.OnPreviewMouseDown(e);
        }
    }

    /// <summary>
    /// 乘法转换器，value*parameter
    /// </summary>
    public class MultiConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (double)parameter * (double)value; 
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
